package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.whmnx.base.adapter.DynamicAdapter2;
import com.whmnx.base.bean.OrderDetailBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.UItils;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.FragmentOrderDetailsBinding;
import com.whmnx.my.ui.fragment.MyOrderDetailsFragment;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/15 0015 17:37
 * version: 1.0
 * @author Administrator
 */
public class OrderDetailsModel extends ToolbarViewModel{
    public ObservableField<String> Address_Name = new ObservableField<>("");
    public ObservableField<String> Address_Mobile = new ObservableField<>("");
    public ObservableField<String> Address_Label = new ObservableField<>("");
    public ObservableField<String> totalAmount = new ObservableField<>("");
    public ObservableField<String> reducedAmount = new ObservableField<>("");
    public ObservableField<String> payAmount = new ObservableField<>("");
    public ObservableField<String> PayDesc = new ObservableField<>("");
    public ObservableField<String> CreateDate = new ObservableField<>("");
    public ObservableField<String> No = new ObservableField<>("");
    public ObservableField<String> PayDate = new ObservableField<>("");
    //最上面
    public ObservableField<String> PayType2 = new ObservableField<>("");
    public ObservableField<String> PayType = new ObservableField<>("");


    private MyOrderDetailsFragment mFragment;
    private FragmentOrderDetailsBinding mBinding;
    private DynamicAdapter2 mDynamicAdapter;
    private Disposable subscribe;

    public OrderDetailsModel(@NonNull Application application) {
        super(application);
    }


    public void initToolbar(MyOrderDetailsFragment myOrderDetailsFragment, FragmentOrderDetailsBinding binding) {
        this.mFragment = myOrderDetailsFragment;
        this.mBinding = binding;
        setTitleText("订单详情");
        setLeftIconSetVisible(View.VISIBLE);
        setRightIconSetVisible(View.GONE);
        setRightIconVisible(View.GONE);

        mBinding.rvList.setLayoutManager(new LinearLayoutManager(mFragment.getActivity()));
        mBinding.rvList.setItemAnimator(null);
        mDynamicAdapter = new DynamicAdapter2(mFragment.getActivity());
        mBinding.rvList.setAdapter(mDynamicAdapter);
        mBinding.rvList.setFocusableInTouchMode(false);

        getOrderdetail();
        initView();

    }


    private void initView() {
        subscribe = RxBus.getDefault().toObservable(String.class)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String object){
                        if (object.equals(EventConstants.HUFU)){  //支付回调)
                           finish();
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    private void getOrderdetail() {
        Map<String, Object> params = new HashMap<>();
        params.put("OrderId",mFragment.orderId);
        HttpClient.get(mFragment.getActivity(), Constants.ORDERDETAIL, params,new CallBack<OrderDetailBean>() {
            @Override
            public void onSuccess(OrderDetailBean result) {
                Address_Name.set("收货人：" + result.getAddress().getAddress_Name());
                Address_Mobile.set(result.getAddress().getAddress_Mobile());
                Address_Label.set( result.getAddress().getAddress_Detailed());

                totalAmount.set("￥" + result.getTotalAmount());
                reducedAmount.set("￥" + result.getReducedAmount());
                reducedAmount.set("￥" + result.getReducedAmount());
                payAmount.set("￥" + result.getPayAmount());
                PayDesc.set(result.getPayDesc());
                CreateDate.set("下单时间：" + result.getCreateDate().split(" ")[0]);
                if (!TextUtils.isEmpty(result.getPayDate())){
                    PayDate.set("付款时间：" + result.getPayDate().split(" ")[0]);
                }
                No.set("订单编号：" + result.getNo());
                PayType2.set(result.orderStatusDesc);
                //订单状态  最下面
                PayType.set(UItils.getOrderState(result.orderStatus));

                SPUtils.getInstance().put("no", result.getNo());
                SPUtils.getInstance().put("mId", result.getMId());
                mDynamicAdapter.replaceAll(result.getProductList(), true);


              //  observableList.clear();
              /*  for (OrderDetailBean.ProductListBean productListBean : result.getProductList()) {
                    observableList.add(new OrderItemViewModel4(OrderDetailsModel.this,productListBean));
                }*/
            }
        });
    }


    //给RecyclerView添加ObservableList
    public ObservableList<OrderItemViewModel4> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<OrderItemViewModel4> itemBinding = ItemBinding.of(BR.viewModel, R.layout.grid_order6);
}
